/*
 * CommentListPresenter
 * 
 * Copyright 2011 Matt Whipple <matt@mattwhipple.com>
 * 
 * This file is part of com.teamsuperfitness.apps
 * 
 * com.teamsuperfitness.apps is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 * 
 * com.teamsuperfitness.apps is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with com.teamsuperfitness.apps. If not, see http://www.gnu.org/licenses/.
 */

package com.teamsuperfitness.apps.client.presenters;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.teamsuperfitness.apps.client.service.CommentService;
import com.teamsuperfitness.apps.client.service.CommentServiceAsync;
import com.teamsuperfitness.apps.shared.events.LeadSelectedEvent;
import com.teamsuperfitness.apps.shared.events.LeadSelectedEventHandler;
import com.teamsuperfitness.apps.shared.model.IComment;
import com.teamsuperfitness.apps.shared.model.Lead;

/**
 * Allow display and addition of comments
 * @author mwhipple
 */

public class CommentListPresenter implements IPresenter {
	public interface Display {
		Widget asWidget();
		void setData(List<IComment> data);
		HasClickHandlers getInsertComment();
		void prepNewComment();
		IComment getNewComment();
	}

	private Lead currentLead;
	
	private CommentServiceAsync commentService = GWT.create(CommentService.class);

	private Display display;
	private SimpleEventBus eventBus;
	
	public CommentListPresenter(SimpleEventBus eventBus, Display view) {
		this.display = view;
		this.eventBus = eventBus;
		bind();
	}
	
	public void go(HasWidgets container) {
		container.clear();
		container.add(display.asWidget());
	}

	public void bind() {
		eventBus.addHandler(LeadSelectedEvent.TYPE, new LeadSelectedEventHandler() {
			public void onLeadSelected(LeadSelectedEvent event) {
				currentLead = event.getLead();
				updateCommentList();
			}
		});
		
		display.getInsertComment().addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				IComment newComment = display.getNewComment();
				newComment.setParent(currentLead);
				commentService.add(newComment, new AsyncCallback<Void>(){
					public void onFailure(Throwable caught) {}
					public void onSuccess(Void result) {
						updateCommentList();
					}
				});
				display.prepNewComment();
			}});
	}

	public void updateCommentList() {
		commentService.getFor(currentLead, new AsyncCallback<List<IComment>>() {
			public void onFailure(Throwable caught) {}
			public void onSuccess(List<IComment> result) {
				display.setData(result);
			}});
	}
}
